package com.reform.utils;


import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class VerifycUtils {

    /**
     * 验证数据是否为空格或者前端参数传入为“”
     *
     * @param objects
     * @return
     */
    public static boolean verifyNullOrEmptyUtils(Object... objects) {
        for (Object object : objects) {
            if (object == null) {
                return false;
            }
            if (object instanceof String) {
                if (((String) object).isEmpty())
                    return false;
            }
        }
        return true;
    }

    /**
     * 验证类型值或工号长度是否有误
     *
     * @param gh
     * @param type
     * @return
     */
    public static boolean verifyTypeValueOrGhLength(String gh, Integer type) {
        if (gh.length() != 5 && gh.length() != 6) {
            return false;
        }
        if (type <= 0 || type > 3) {
            return false;
        }
        return true;
    }

    /**
     * 验证邮箱是否合法
     *
     * @param email
     * @return
     */
    public static boolean verifyEmailISLegal(String email) {
        if (email == null || email.isEmpty()) {
            return false;
        }
        return Pattern.matches("^(\\w+([-.][A-Za-z0-9]+)*){3,18}@\\w+([-.][A-Za-z0-9]+)*\\.\\w+([-.][A-Za-z0-9]+)*$", email);
    }

    /**
     * 校验验证码长度与是否为空
     *
     * @param code
     * @return
     */
    public static boolean verifyEmailCode(String code) {
        if (code == null || code.isEmpty()) {
            return false;
        }
        if (code.length() != 6) {
            return false;
        }
        return true;
    }

    /**
     * 严格的日期格式校验
     * 1、日期格式严格为yyyy-MM-dd
     *
     * @param str
     * @return
     */
    public static boolean isValidDate(String str) {
        DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        formatter.setLenient(false);
        try {
            Date date = formatter.parse(str);
            return !str.equals(formatter.format(date));
        } catch (Exception e) {
            return true;
        }
    }

    public static boolean verifyDate(String sDate) {
        String datePattern1 = "\\d{4}-\\d{2}-\\d{2}";
        String datePattern2 = "^((\\d{2}(([02468][048])|([13579][26]))"
                + "[\\-\\/\\s]?((((0?[13578])|(1[02]))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|"
                + "(3[01])))|(((0?[469])|(11))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(30)))|(0?2[\\-\\/\\s]?"
                + "((0?[1-9])|([1-2][0-9])))))|(\\d{2}(([02468][1235679])|([13579][01345789]))[\\-\\/\\s]?("
                + "(((0?[13578])|(1[02]))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])|(11))[\\-\\/\\s]?"
                + "((0?[1-9])|([1-2][0-9])|(30)))|(0?2[\\-\\/\\s]?((0?[1-9])|(1[0-9])|(2[0-8]))))))";
        if ((sDate != null)) {
            Pattern pattern = Pattern.compile(datePattern1);
            Matcher match = pattern.matcher(sDate);
            if (match.matches()) {
                pattern = Pattern.compile(datePattern2);
                match = pattern.matcher(sDate);
                return match.matches();
            } else {
                return false;
            }
        }
        return false;
    }

    public static void changeNULL(String... strings) {
        for (int i = 0; i < strings.length; i++) {
            if (strings[i].isEmpty())
                strings[i] = null;
        }
    }
}
